package com.erp.admin.business.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.base.base.BaseController;
import com.erp.base.base.Page;
import com.erp.base.business.entity.ExpertApprovalRecord;
import com.erp.base.business.service.ExpertApprovalRecordService;
import com.erp.base.common.utils.R;

/**
 * 申请专家审批记录表
 * 
 * @author wanghw
 * @email whongwei_yz@sina.com
 * @date 2018-10-22 11:31:31
 */

@Controller
@RequestMapping("/business/expertApprovalRecord")
public class ExpertApprovalRecordController extends BaseController {
	@Autowired
	private ExpertApprovalRecordService expertApprovalRecordService;

	@GetMapping()
	@RequiresPermissions("business:expertApprovalRecord:expertApprovalRecord")
	String ExpertApprovalRecord() {
		return "business/expertApprovalRecord/expertApprovalRecord";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("business:ExpertApprovalRecord:expertApprovalRecord")
	public Page<ExpertApprovalRecord> list(
			ExpertApprovalRecord expertApprovalRecord,
			HttpServletRequest request, HttpServletResponse response) {
		// 查询列表数据 只展示未审批单子
		Page<ExpertApprovalRecord> page = Page.bootstrapPage(request, response);
		expertApprovalRecord.setStatus("0");
		page = expertApprovalRecordService.findPage(page, expertApprovalRecord);
		return page;
	}

	@GetMapping("/add")
	@RequiresPermissions("business:expertApprovalRecord:add")
	String add() {
		return "business/expertApprovalRecord/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("business:expertApprovalRecord:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		ExpertApprovalRecord expertApprovalRecord = expertApprovalRecordService.get(id);
		model.addAttribute("expertApprovalRecord", expertApprovalRecord);
		return "business/expertApprovalRecord/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("business:expertApprovalRecord:add")
	public R save(ExpertApprovalRecord expertApprovalRecord, Model model) {
		try {
			expertApprovalRecordService.save(expertApprovalRecord);
			return R.ok();
		} catch (Exception e) {
			logger.error("保存失败", e);
			return R.error();
		}
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("business:expertApprovalRecord:edit")
	public R update(ExpertApprovalRecord expertApprovalRecord, Model model) {
		try {
			expertApprovalRecordService.updateExpert(expertApprovalRecord);
			return R.ok();
		} catch (Exception e) {
			logger.error("保存失败", e);
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("business:expertApprovalRecord:remove")
	public R remove(Long id) {
		try {
			expertApprovalRecordService.delete(id);
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败， id:" + id, e);
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("business:expertApprovalRecord:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		try {
			expertApprovalRecordService.deleteBatch(Arrays.asList(ids));
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败，ids:" + ids, e);
			return R.error();
		}

	}

}
